﻿using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NReader.Model;
using NReader.Model.RAS;

namespace Local.Service
{
    public class LocalXmlService
    {
        #region index

        public static async Task SaveSysWebInfo(IList<SysWebInfo> sysWebInfos)
        {
            await Utils.CheckFolder("global");
            await XmlHelper.Serialize(sysWebInfos, @"global\webinfos");
        }

        public static async Task<IList<SysWebInfo>> GetSysWebInfos()
        {
            return await XmlHelper.Deserialize<IList<SysWebInfo>>(@"global\webinfos");
        }

        public static async Task<SysWebInfo> GetSysWebInfo(string webflag)
        {
            var list = await XmlHelper.Deserialize<IList<SysWebInfo>>(@"global\webinfos");
            var item = from p in list
                       where p.webflag.Equals(webflag)
                       select p;
            if (item.Any())
            {
                return item.First();
            }
            return null;
        }

        public static async Task SaveIndexGroup(IList<ArticleInfo> list)
        {
            await Utils.CheckFolder("global");
            await XmlHelper.Serialize(list, @"global\index");
        }

        public static async Task<IList<ArticleInfo>> GetIndexGroup()
        {
            return await XmlHelper.Deserialize<IList<ArticleInfo>>(@"global\index");
        }

        #endregion

        #region book

        public static async Task SaveMaxChapters(int articleid, IList<ChapterInfo> list)
        {
            await Utils.CheckFolder("maxs");
            await XmlHelper.Serialize(list, string.Format(@"maxs\{0}", articleid));
        }

        public static async Task<IList<ChapterInfo>> GetMaxChapters(int artilceid)
        {
            return await XmlHelper.Deserialize<IList<ChapterInfo>>(string.Format(@"maxs\{0}", artilceid));
        }

        #endregion

        #region bookreader

        public static async Task SaveBookReader(int articleid,string webflag, IList<ChapterInfo> list)
        {
            await Utils.CheckFolder("bookreader");
            await Utils.CheckFolder(string.Format(@"bookreader\{0}", webflag));
            await XmlHelper.Serialize(list, string.Format(@"bookreader\{0}", webflag), string.Format(@"{0}",articleid));
        }

        public static async Task<IList<ChapterInfo>> GetBookReader(int artilceid, string webflag)
        {
            return
                await
                XmlHelper.Deserialize<IList<ChapterInfo>>(string.Format(@"bookreader\{0}", webflag),string.Format(@"{0}",artilceid));
        }

        #endregion

        #region chapter

        //public static async Task SaveChapter(ChapterInfo chapter)
        //{
        //    await Utils.CheckFolder("session");
        //    await XmlHelper.Serialize(chapter, string.Format(@"session\{0}", chapter.chapterid));
        //}

        //public static async Task<ChapterInfo> GetChapter(int chapterid)
        //{
        //    return await XmlHelper.Deserialize<ChapterInfo>(string.Format(@"session\{0}", chapterid));
        //}

        public static async Task SaveChapter(ChapterInfo chapter)
        {
            await Utils.CheckFolder("chapter");
            await Utils.CheckFolder(string.Format(@"chapter\{0}", chapter.articleid));
            await XmlHelper.Serialize(chapter, string.Format(@"chapter\{0}\{1}", chapter.articleid, chapter.chapterid));
        }

        public static async Task<ChapterInfo> GetChapter(int articleid,int chapterid)
        {
            await Utils.CheckFolder("chapter");
            await Utils.CheckFolder(string.Format(@"chapter\{0}", articleid));
            return
                await
                XmlHelper.Deserialize<ChapterInfo>(string.Format(@"chapter\{0}\{1}", articleid,chapterid));
        }

        #endregion

        #region global


        /// <summary>
        /// 序列化保存设置
        /// </summary>
        /// <param name="setting"></param>
        /// <returns></returns>
        public static async Task SaveSetting(SettingInfo setting)
        {
            await Utils.CheckFolder("global");
            await XmlHelper.Serialize(setting, @"global\setting");
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static async Task<SettingInfo> GetSetting()
        {
            return await XmlHelper.Deserialize<SettingInfo>(@"global\setting");
        }

        #endregion
    }
}
